﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Telerik.Web.UI;

namespace Sistema_de_Taller.SDT_UI.Client
{
    public partial class client_Details : System.Web.UI.UserControl
    {

        public static DataTable VehiclesTMP;
        public static UInt32 ClientID;

        private void Page_Init(object sender, System.EventArgs e)
        {

            ClientID = Convert.ToUInt32(Request.QueryString["Id"]);
            //DataTable Vehicle;
            cargarForms(ClientID);
            if (!IsPostBack)
            {
                tableLoad(ClientID);
                
               // VehiclesTMP = CreateDataTable();
               // Session["myDatatable"] = VehiclesTMP;
                this.RadGrid1.DataSource = tableLoad(ClientID);
            }

            this.RadGrid1.Rebind();

        }

        protected void grdWorkOrder_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem edititem = e.Item as GridEditableItem;
            String WorkOrderID = edititem["WorkOrder_ID"].Text;
            String query = string.Format("~/SDT_UI/WorkOrder/DetailsWorkOrder.aspx?Id={0} ", WorkOrderID);
            Response.Redirect(query);
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem edititem = e.Item as GridEditableItem;
            String ClientID = edititem["Client_ID"].Text;
            String query = string.Format("~/SDT_UI/Client/ClientDetails.aspx?Id={0} ", ClientID);
            Response.Redirect(query);
        }

        protected void dpSellDate_Load(object sender, EventArgs e)
        {
            dpSellDate.SelectedDate = DateTime.Today;
        }

        private DataTable tableLoad(UInt32 ClientID)
        {
            DataTable Vehicle;
            VehiclesTMP = CreateDataTable();
            SDT_DAL.Utilities.Vehicle_DAL VehicleData = new SDT_DAL.Utilities.Vehicle_DAL();
            Vehicle = VehicleData.VehicleLoadID(ClientID).Tables[0];

            foreach (DataRow Fila in Vehicle.Rows)
            {
                DataRow Row = VehiclesTMP.NewRow();
                Row["ID"] = Convert.ToUInt32(Fila[0]);
                Row["Marca"] = Fila[1].ToString();
                Row["Modelo"] = Fila[2].ToString();
                Row["Patente"] = Fila[3].ToString();
                Row["Número de Motor"] = Fila[4].ToString();
                Row["Número de Serie"] = Convert.ToUInt32(Fila[5]);
                Row["Fecha de Venta"] = Convert.ToDateTime(Fila[6]);
                Row["Estado"] = 0;
                
                //SDT_DAL.Utilities.Vehicle_DAL modifico = new SDT_DAL.Utilities.Vehicle_DAL();
                VehiclesTMP.Rows.Add(Row);
            }
            return VehiclesTMP;
        }

        private void cargarForms(UInt32 ClientID)
        {
            DataSet Client, Vehicle;

            SDT_DAL.Utilities.Client_DAL ClientData = new SDT_DAL.Utilities.Client_DAL();
            SDT_DAL.Utilities.Vehicle_DAL VehicleData = new SDT_DAL.Utilities.Vehicle_DAL();
            Client = ClientData.ClientLoadID(ClientID);
            Vehicle = VehicleData.VehicleLoadID(ClientID);
            //Cargo datos Cliente
            txtName.Text = Client.Tables[0].Rows[0].ItemArray[0].ToString().Trim();
            txtSocial.Text = Client.Tables[0].Rows[0].ItemArray[1].ToString().Trim();
            txtState.Text = Client.Tables[0].Rows[0].ItemArray[2].ToString().Trim();
            txtResidence.Text = Client.Tables[0].Rows[0].ItemArray[3].ToString().Trim();
            txtMail.Text = Client.Tables[0].Rows[0].ItemArray[4].ToString().Trim();
            //txtPhone.Text = edititem["Client_Phone"].Text;
            dpDateUp.SelectedDate = Convert.ToDateTime(Client.Tables[0].Rows[0].ItemArray[5]);
            txtCountry.Text = Client.Tables[0].Rows[0].ItemArray[6].ToString().Trim();
            txtProvince.Text = Client.Tables[0].Rows[0].ItemArray[7].ToString().Trim();
            txtCity.Text = Client.Tables[0].Rows[0].ItemArray[8].ToString().Trim();
            txtPostCode.Text = Client.Tables[0].Rows[0].ItemArray[9].ToString().Trim();
           // Client_ID = Convert.ToUInt32(edititem["Client_ID"].Text);
        
        
        
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.RadGrid1.Rebind();
        }

        protected void btnUpdateClient_Click(object sender, EventArgs e)
        {
            Common_Files.Definitions.Client Client = new Common_Files.Definitions.Client();

            Client.Client_Name = txtName.Text;
            Client.Client_Social = txtSocial.Text;
            Client.Client_State = txtState.Text;
            Client.Client_Residence = txtResidence.Text;
            Client.Client_Email = txtMail.Text;
            Client.Client_DateUp = dpDateUp.SelectedDate.Value;
            Client.Client_Country = txtCountry.Text;
            Client.Client_Province = txtProvince.Text;
            Client.Client_City = txtCity.Text;
            Client.Client_PostCode = txtPostCode.Text;
            Client.Client_Phone = txtPhone.Text;
            Client.Client_ID = ClientID;

            SDT_DAL.Utilities.Client_DAL modifico = new SDT_DAL.Utilities.Client_DAL();

            Client.Client_ID= modifico.ClientUpdate(Client);

            updateVehicle(Client.Client_ID);

           // lblMessages.Text = "Cliente cargado satisfactoriamente";

        }

        public void updateVehicle(UInt32 Client_ID)
        {
            foreach (DataRow Fila in VehiclesTMP.Rows)
            {
                if (Fila["Estado"].ToString() == "2")
                {   //Es un auto nuevo
                    Common_Files.Definitions.Vehicle Vehicle = new Common_Files.Definitions.Vehicle();
                    Vehicle.Client_ID = Client_ID;
                    Vehicle.Vehicle_Brand = Fila["Marca"].ToString();
                    Vehicle.Vehicle_Model = Fila["Modelo"].ToString();
                    Vehicle.Vehicle_Patent = Fila["Patente"].ToString();
                    Vehicle.Vehicle_EngineNumber = Fila["Número de Motor"].ToString();
                    Vehicle.Vehicle_SerialNumber = Convert.ToUInt32(Fila["Número de Serie"]);
                    Vehicle.Vehicle_SellDate = Convert.ToDateTime(Fila["Fecha de Venta"]);
                    SDT_DAL.Utilities.Vehicle_DAL inserto = new SDT_DAL.Utilities.Vehicle_DAL();
                    inserto.VehicleInsert(Vehicle);
                    //modifico.VehicleUpdate(Vehicle);
                }
            }
        }

        protected void btnAddVehicle_Click(object sender, EventArgs e)
        {
            this.RadGrid1.DataSource = VehiclesTMP;
            this.RadGrid1.Rebind();
            pnlAddVehicle.Visible = true;
        }

        protected void dgvVehicles_Load(object sender, EventArgs e)
        {

        }

        private DataTable AddVehicle()
        {
            //DataTable VehiclesTMP = new DataTable();
            // VehiclesTMP = CreateDataTable();
            DataRow Fila = VehiclesTMP.NewRow();
            Fila["Marca"] = cmbBrand.SelectedValue.ToString();
            Fila["Modelo"] = txtModel.Text;
            Fila["Patente"] = txtPatent.Text;
            Fila["Número de Serie"] = Convert.ToUInt32(txtSerialNumber.Text);
            Fila["Número de Motor"] = txtEngineNumber.Text;
            Fila["Fecha de Venta"] = dpSellDate.SelectedDate;
            Fila["Estado"] = 2;
            VehiclesTMP.Rows.Add(Fila);
            return (VehiclesTMP);
        }

        protected void btnNewVehicle_Click(object sender, EventArgs e)
        {
            DataTable VehiclesTMP = AddVehicle();
            this.RadGrid1.DataSource = VehiclesTMP;
            this.RadGrid1.Rebind();

        }

        private DataTable CreateDataTable()
        {
            DataTable VehiclesTMP = new DataTable();

            DataColumn Vehicle_Brand;
            DataColumn Vehicle_Patent;
            DataColumn Vehicle_EngineNumber;
            DataColumn Vehicle_Model;
            DataColumn Vehicle_SerialNumber;
            DataColumn Vehicle_SellDate;
            DataColumn Vehicle_State;
            DataColumn Vehicle_ID;

            Vehicle_ID = new DataColumn();
            Vehicle_ID.DataType = Type.GetType("System.UInt32");
            Vehicle_ID.ColumnName = "ID";
            VehiclesTMP.Columns.Add(Vehicle_ID);

            Vehicle_Brand = new DataColumn();
            Vehicle_Brand.DataType = Type.GetType("System.String");
            Vehicle_Brand.ColumnName = "Marca";
            VehiclesTMP.Columns.Add(Vehicle_Brand);

            Vehicle_Patent = new DataColumn();
            Vehicle_Patent.DataType = Type.GetType("System.String");
            Vehicle_Patent.ColumnName = "Patente";
            VehiclesTMP.Columns.Add(Vehicle_Patent);

            Vehicle_EngineNumber = new DataColumn();
            Vehicle_EngineNumber.DataType = Type.GetType("System.String");
            Vehicle_EngineNumber.ColumnName = "Número de Motor";
            VehiclesTMP.Columns.Add(Vehicle_EngineNumber);

            Vehicle_Model = new DataColumn();
            Vehicle_Model.DataType = Type.GetType("System.String");
            Vehicle_Model.ColumnName = "Modelo";
            VehiclesTMP.Columns.Add(Vehicle_Model);

            Vehicle_SerialNumber = new DataColumn();
            Vehicle_SerialNumber.DataType = Type.GetType("System.UInt32");
            Vehicle_SerialNumber.ColumnName = "Número de Serie";
            VehiclesTMP.Columns.Add(Vehicle_SerialNumber);

            Vehicle_SellDate = new DataColumn();
            Vehicle_SellDate.DataType = Type.GetType("System.DateTime");
            Vehicle_SellDate.ColumnName = "Fecha de Venta";
            VehiclesTMP.Columns.Add(Vehicle_SellDate);

            //Columna estado:
            // Si es 0, no modifico, si es 1, hago update, si es 2 hago insert
            Vehicle_State = new DataColumn();
            Vehicle_State.DataType = Type.GetType("System.UInt16");
            Vehicle_State.ColumnName = "Estado";
            VehiclesTMP.Columns.Add(Vehicle_State);

            return VehiclesTMP;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            pnlAddVehicle.Visible = false;
            this.RadGrid1.Rebind();
        }
    }
}